<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .w {
            width: 1226px;
            margin: 0 auto;
        }

        .header {
            height: 200px;
            background-color: grey;
        }

        .banner {
            height: 400px;
            background-color: pink;
        }

        .main .part {
            height: 400px;
            border: 1px solid #333;
            text-align: center;
            line-height: 400px;
            box-sizing: border-box;
        }

        .footer {
            height: 500px;
            background-color: #333;
        }

        .asideNav {
            /* width: 40px; */
            font-size: 12px;
            text-align: center;
            line-height: 40px;
            padding: 10px 5px;
            background-color: #f6f6f6;
            position: absolute;
            right: 50px;
            top: 200px;
            /* transition: top 0.5s; */
        }

        .asideNav li {
            border-bottom: 1px solid #333;
            padding: 0 10px;
        }

        .asideNav li:hover {
            background-color: #00a1d6;
            color: #fff;
        }

        .asideNav .active {
            background-color: #00a1d6;
            color: #fff;
        }

        .asideNav.fixed {
            position: fixed;
            top: 50%;
            transform: translateY(-50%);
        }
    </style>
</head>

<body>
    <!-- .header+.banner+(.main>.part*10)+.footer -->
    <div class="header w"></div>
    <div class="banner w"></div>
    <div class="main w">
        <div class="part">part_01</div>
        <div class="part">part_02</div>
        <div class="part">part_03</div>
        <div class="part">part_04</div>
        <div class="part">part_05</div>
        <div class="part">part_06</div>
        <div class="part">part_07</div>
        <div class="part">part_08</div>
        <div class="part">part_09</div>
        <div class="part">part_10</div>
    </div>
    <div class="footer"></div>

    <div class="asideNav">
        <div class="nav">
            <li>nav_01</li>
            <li>nav_02</li>
            <li>nav_03</li>
            <li>nav_04</li>
            <li>nav_05</li>
            <li>nav_06</li>
            <li>nav_07</li>
            <li>nav_08</li>
            <li>nav_09</li>
            <li>nav_10</li>
            <div class="back">top</div>
        </div>
    </div>
</body>
<script>
    // bilibili侧边栏导航
    // 1. 头部移出页面 => 侧边栏导航变为固定效果
    // 2. 点击侧边栏导航 => 滚动条滚动值对应盒子的位置
    // 3. 页面滚动  => 导航可以跟着盒子对应的区域滚动
    // 4. 回到顶部   

    var header = document.getElementsByClassName("header")[0];
    var main = document.getElementsByClassName("main")[0];
    var asideNav = document.getElementsByClassName("asideNav")[0];
    var back = document.getElementsByClassName("back")[0];

    var liList = document.querySelectorAll(".nav li");
    var partList = document.querySelectorAll(".main .part");

    scrollHandler();  //页面加载时默认执行一次
    window.onscroll = scrollHandler; // 每次滚动再次执行

    for (let i = 0; i < liList.length; i++) {
        let li = liList[i];
        li.onclick = function () {
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
            }
            liList[i].className = "active";

            var scrollTop = partList[i].offsetTop;
            console.log(scrollTop);

            // document.documentElement.scrollTop = scrollTop;

            // window.scrollTo(x,y)  将页面的滚动跳滚动至指定位置
            // window.scrollTo(0, scrollTop);

            window.scrollTo({
                // left:0,
                top: scrollTop,
                behavior: "smooth",   //平滑滚动
            });

        }
    }

    back.onclick = function () {
        window.scrollTo({
            // left:0,
            top: 0,
            behavior: "smooth",   //平滑滚动
        });
    }




    function scrollHandler() {
        var scrollTop = document.documentElement.scrollTop;
        if (scrollTop >= header.clientHeight) {
            // asideNav.style.position = "fixed";
            // asideNav.style.top = "50%";
            // asideNav.style.transform = "translateY(-50%)";
            asideNav.classList.add("fixed");

        } else {
            asideNav.classList.remove("fixed");
        }


        // 每次滚动时  判断滚动的区间是否在对应的导航内

        if (scrollTop >= main.offsetTop && scrollTop <= main.offsetTop + main.clientHeight) {
            for (let i = 0; i < partList.length; i++) {
                let part = partList[i]; // 导航对应的内容区域
                var start = part.offsetTop; //盒子距离顶部的偏移;
                var end = part.offsetTop + part.clientHeight;

                if (scrollTop >= start && scrollTop <= end) {
                    for (var j = 0; j < liList.length; j++) {
                        liList[j].className = "";
                    }
                    liList[i].className = "active";
                }

            }

        } else {
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
            }
        }



    }




</script>

</html>